package com.cw.carwashstoress.controller;

import java.util.List;

import com.cw.carwashstoress.service.StoreService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.cw.carwashstoress.domain.Store;

/**
 * 门店管理 控制器
 */
@RestController
@RequestMapping("/store")
public class StoreController extends BaseController {
    @Autowired
    private StoreService storeService;

    /**
     * 获取门店列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Store store) {
        startPage();
        List<Store> list = storeService.selectStoreList(store);
        return getDataTable(list);
    }

    /**
     * 根据门店ID获取详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(storeService.selectStoreById(id));
    }

    /**
     * 新增门店
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Store store) {
        if (!storeService.checkStoreNameUnique(store)) {
            return AjaxResult.error("新增门店'" + store.getStoreName() + "'失败，该城市已有同名门店");
        }
        return toAjax(storeService.insertStore(store));
    }

    /**
     * 修改门店
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Store store) {
        if (!storeService.checkStoreNameUnique(store)) {
            return AjaxResult.error("修改门店'" + store.getStoreName() + "'失败，该城市已有同名门店");
        }
        return toAjax(storeService.updateStore(store));
    }

    /**
     * 删除门店
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(storeService.deleteStoreByIds(ids));
    }

    /**
     * 根据城市ID查询门店列表
     */
    @GetMapping("/city/{cityId}")
    public AjaxResult listByCityId(@PathVariable Integer cityId) {
        List<Store> list = storeService.selectStoreListByCityId(cityId);
        return AjaxResult.success(list);
    }

    /**
     * 更新门店订单统计信息
     */
    @PutMapping("/stats")
    public AjaxResult updateStats(@RequestBody Store store) {
        return toAjax(storeService.updateStoreOrderStats(store));
    }

} 